﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Science.Mathematics.Algebra
{
    public class MathHelper
    {
        #region Public Static Function

        public static int GCF(int num1, int num2)
        {
            List<int> numeratorFactors = FindPrimeFactors(num1);
            List<int> denomeratorFactors = FindPrimeFactors(num2);
			int result = 1;
            foreach (int value in numeratorFactors)
            {
                if (denomeratorFactors.Contains(value))
                {
                    result *= value;
                    denomeratorFactors.Remove(denomeratorFactors.First(x => x == value));
                }
            }
			return result;
        }

        public static int LCM(int num1, int num2)
		{
            List<int> num1_multipliers = new List<int>();
            List<int> num2_multipliers = new List<int>();

			int index = 0;
			var intersect = num1_multipliers.Intersect(num2_multipliers) ;                
			while (intersect.Count() == 0)
			{
				num1_multipliers.Add(num1 + num1 * index);
				num2_multipliers.Add(num2 + num2 * index);
                intersect = num1_multipliers.Intersect(num2_multipliers);   
				index++;
			}
			return intersect.First();
		}

        public static List<int> FindPrimeFactors(int num)
        {
            List<int> factors = new List<int>();
            while (Math.Abs(num) != 1)
            {
                int divisor = 2;
                while (num % divisor != 0 && divisor < Math.Abs(num))
                    divisor++;
                num = num / divisor;
                factors.Add(divisor);
            }
            return factors;
        }

        public static int DoubleCheck(double d1, double d2)
        {
            if (Math.Abs(d1 - d2) < Double.Epsilon)
                return 0;
            return d1 > d2 ? 1 : -1;
        }

        #endregion
    }
}
